<?php
declare (strict_types=1);

namespace app\admin\model\data;

use app\common\model\DeviceType;
use app\user\model\user\Account;
use think\model\relation\BelongsTo;

/**
 * 子订单统计
 */
class PaymentOrderItem extends DataModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'payment_order_item';

    /**
     * 来源
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     *
     * @param string $value
     * @param        $data
     * @return string
     */
    public function getFromTextAttr($value = '', $data)
    {
        return DeviceType::getDeviceTitleByKey($data['from']);
    }

    /**
     * 主订单
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-21
     *
     * @return BelongsTo [type]                         [description]
     */
    public function pdorder()
    {
        return $this->belongsTo(PaymentOrder::class, 'order_no', 'order_no');
    }

    /**
     * 付费用户信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-17
     *
     * @return BelongsTo [type]                         [description]
     */
    public function user()
    {
        return $this->belongsTo(Account::class, 'user_id');
    }

    /**
     * 下单量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     *
     * @param array $timespan [description]
     * @return int [type]                                   [description]
     */
    public static function getPlaceOrderCountByTimespan(array $timespan)
    {
        return PaymentOrderItem::scope('mhm')->whereBetween('create_time', $timespan)->count();
    }

    /**
     * 下单的支付量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-23
     *
     * @param array $timespan [description]
     * @return int [type]                                   [description]
     */
    public static function getPlaceOrderPaymentCountByTimespan(array $timespan)
    {
        return PaymentOrderItem::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['status']])->whereBetween('__TABLE__.create_time', $timespan)->whereIn('pdorder.status', [20, 30, 40])->count();
    }

    /**
     * 某段时间支付订单量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     *
     * @param array 时间区间
     * @return int [type]                                   [description]
     */
    public static function getPaymentOrderCountByTimespan(array $timespan)
    {
        return PaymentOrderItem::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['status', 'payment_time']])->whereBetween('pdorder.payment_time', $timespan)->whereIn('pdorder.status', [20, 30, 40])->count();
    }

    /**
     * 销售额
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     *
     * @param array 时间区间
     * @return float [type]                                   [description]
     */
    public static function getSalePriceByTimespan(array $timespan)
    {
        return PaymentOrderItem::withJoin(['pdorder' => ['status', 'payment_time']])
            ->where('payment_order_item.mhm_id', request()->mhm_id)
            ->where('payment_order_item.product_type_id', '<>', 6)
            ->whereBetween('pdorder.payment_time', $timespan)
            ->whereIn('pdorder.status', [20, 30, 40])
            ->sum('payment_order_item.payment');
    }

    /**
     * 非课程销售额
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     *
     * @param array 时间区间
     * @return float [type]                                   [description]
     */
    public static function getSalePriceByTimespanOfNonCourse(array $timespan)
    {
        return PaymentOrderItem::withJoin(['pdorder' => ['status', 'payment_time']])
            ->whereBetween('pdorder.payment_time', $timespan)
            ->whereIn('pdorder.status', [20, 30, 40])
            ->where('payment_order_item.mhm_id', request()->mhm_id)
            ->whereNotIn('product_type_id', '1,2,3,4')
            ->sum('payment_order_item.payment');
    }

    /**
     * 某段时间付费用户数
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     *
     * @param array 时间区间
     * @return mixed [type]                                   [description]
     */
    public static function getPaymentUserCountByTimespan(array $timespan)
    {
        return PaymentOrderItem::scope('mhm', '__TABLE__.mhm_id')->withJoin(['pdorder' => ['status', 'payment_time']])->whereBetween('pdorder.payment_time', $timespan)->value('COUNT(DISTINCT payment_order_item.user_id)');
    }

    /**
     * 下单用户量
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-16
     *
     * @param array $timespan [description]
     * @return int [type]                                   [description]
     */
    public static function getPlaceOrderUserCountByTimespan(array $timespan)
    {
        return PaymentOrderItem::scope('mhm', '__TABLE__.mhm_id')->whereBetween('create_time', $timespan)->count('DISTINCT user_id');
    }

}
